axios.interceptors.request.use(config=>{
    let token=window.sessionStorage.getItem("token")
    if(token!=null)
    {
        config.headers['Authorization']=token
    }
    return config
},error=>{
    Promise.reject(error)
})

axios.interceptors.response.use(response=>{

    let refToken = response.headers['x-token']
    if(refToken){
        //如果有新的token，则替换原有token
        window.sessionStorage.setItem('token',refToken);
    }

    if(response.data.msg==="token-expired")
    {
        alert("登录过期")
        window.sessionStorage.setItem("loginName",null)
        window.location.href="/api/login.html"
    }
    if(response.data.msg==="no-token")
    {
        alert("请先登录")
        window.location.href="/api/login.html"
    }
    if(response.data.msg==="sign-error"||response.data.msg==="inCorrect")
    {
        alert("身份验证失败")
        window.location.href="/api/login.html"
    }
    if(response.data.msg==="no-permission")
    {
        alert("权限不足")
        window.location="/api/Index.html"
    }
    return response

},error=>{
    return Promise.reject(error)
})